var Enemy = function() {
	this.bounds = {
		h:50,
		w:50
	};

	this.coords = {
		x:0,
		y:0,
		dx: 1,
		dy: 1 
	};

	this.run = false

	this.state = 0;
	this.speed = 1.75;

	this.sprites = null;

	/** Array of projectiles throught by enemy
	@type Array of object
	*/
	this.tProjectiles = [];
	this.lastShoot = 0;
	timeBetweenShoot = 80;

	this.init = function(x,y) {
		this.coords.x = x;
		this.coords.y = y;

		this.spritesInstance = new PlayerSprite();
		this.sprites = this.spritesInstance.idle;
	}

	this.update = function(dt) {

		// Update direction from player
		if(player.coords.x > this.coords.x) {
			// Look left
			this.coords.dx = 1;
		} else {
			// Look right
			this.coords.dx = -1;
		}

		var dist = this.coords.x - player.coords.x;

		// Too close from player
		if (Math.abs(dist) <= this.bounds.w*2) {
			if ( dist > 0 )
				this.input('left');
			else if (dist < 0) 
				this.input('right');
		}
		// Distance for shooting
		else if (Math.abs(dist) <= this.bounds.w*4) {
			this.input('shoot');
		}
		// Too far from player
		else {
			if ( dist < 0 )
				this.input('left');
			else if (dist > 0) 
				this.input('right');
			else
				this.input('shoot');
		}

	}


	this.render = function(ctx) {
		animateSprites(this);
		// Save current context
		ctx.save();
		// Scale according to player's direction
		ctx.scale(-this.coords.dx, 1);
		// Render sprites in the appropriate direction
		renderSprites(this);

		// Restore context
		ctx.restore();
	}

	this.input = function(action) {
		var top = 'idle';
		var bottom = 'idle'; 
		this.run = false;

		switch (action) {
			case 'left' : 	if(this.moveLeft() === true) {
								bottom = 'run';
								this.run = true;
							}
							break;

			case 'right' : 	if(this.moveRight() === true) {
								bottom = 'run';
								this.run = true;
							}
							break;

			case 'shoot' : 	if(this.shoot() === true)
								top = 'shoot';
							else
								this.lastShoot++;
							break;

			case 'idle' : 	break;
		}

		this.sprites = this.spritesInstance.instance(top, bottom);
	}

	this.moveRight = function() {
		this.coords.dx = -1;
		return this.moveHorizontaly();
	}

	this.moveLeft = function() {
		this.coords.dx = 1;
		return this.moveHorizontaly();
	}

	this.shoot = function() {
		if (this.lastShoot == 0) {
			var projectile = new Bullet();
			projectile.speed = 2;
			// this.tProjectiles[0] = projectile.init(this); // Debug
			BulletManager.entities.push(projectile.init(this));
		}

		this.lastShoot++;

		if (this.lastShoot > timeBetweenShoot)
			this.lastShoot = 0;

		return true;
	}

	this.moveHorizontaly = function() {
		var x = this.coords.x + this.speed * this.coords.dx;
		var x_abs = x < 0 ? -x : x;
		this.coords.x = x;

		return true;
	}

	this.isInBounds = function(bounds) {
		var x = this.bounds.w/2 + this.coords.x + this.speed * this.coords.dx;
		if(x > 0 || Math.abs(x) >= bounds.w - this.bounds.w) {
			return false;
		}

		return true;
	}
};